package proxy.smart;

/**
 * 代理搜索 将搜索传递给RealSearcher并对调用进行验证和计数
 * 
 * @version 2009-6-29
 * @author Winty(wintys@gmail.com) http://wintys.blogjava.net
 */
public class ProxySearcher implements Searcher {
	private Searcher searcher;
	private String user;

	public ProxySearcher(String user) {
		searcher = new RealSearcher();
		this.user = user;
	}

	@Override
	public String search(String value) {

		if (AccessValidator.validate(user)) {
			String result = searcher.search(value);
			UsageLogger.log(user, value);

			return result;
		}

		return null;
	}
}

/**
 * 对客户端的访问进行验证
 */
class AccessValidator {

	public static boolean validate(String user) {
		boolean permission = false;

		if ("admin".equals(user))
			permission = true;

		return permission;
	}

}

/**
 * 访问计数
 */
class UsageLogger {
	/**
	 * @param user
	 *            用户名
	 * @param value
	 *            搜索的内容
	 */
	public static void log(String user, String value) {
		String sql;
		sql = "INSERT INTO usage(user , value)  values('";
		sql += user + "' , '" + value + "')";
		// 执行SQL: stmt.executeUpdate(sql);
	}
}
